PKGNAME = @PROJECTNAME@
TARGET = @PROJECTNAME@
MODULE = $(TARGET).la

# Various configurable paths (remember to edit Makefile.in, not Makefile)
prefix = @prefix@
exec_prefix = @exec_prefix@
datarootdir = @datarootdir@
datadir = @datadir@
bindir = @bindir@
includedir = @includedir@
libdir =  @libdir@
mandir = @mandir@
libtool = @libtool@
sysconfdir = @sysconfdir@
NETOPEER_DIR = @NETOPEER_DIR@

CC = @CC@
INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL_DATA@
LIBS = @LIBS@
CFLAGS = -Wall @CFLAGS@
CPPFLAGS = @CPPFLAGS@ -DNETOPEER_DIR=\"$(NETOPEER_DIR)\"
LIBTOOL = $(libtool) --tag=CC --quiet
NETOPEER_MANAGER = @NETOPEER_MANAGER@

MODEL = model/ietf-system.yin \
	model/iana-crypt-hash.yin \
	model/ietf-system-data.rng \
	model/ietf-system-gdefs.rng \
	model/ietf-system-schematron.xsl

SRCS = $(TARGET).c \
	base/common.c \
	base/date_time.c \
	base/platform.c \
	base/dns_resolver.c \
	base/shutdown.c \
	base/encrypt.c \
	base/salt.c \
	base/local_users.c
OBJDIR = .obj
LOBJS = $(SRCS:%.c=$(OBJDIR)/%.lo)

all: $(MODULE) $(TARGET)-init

$(TARGET)-init: $(SRCS) $(TARGET)-init.c
	$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ $^ $(LIBS)

*.la: $(LOBJS)
	$(LIBTOOL) --mode=link $(CC) $(CFLAGS) $(CPPFLAGS) $(LIBS) -avoid-version -module -shared -export-dynamic --mode=link -o $@ $^ -rpath $(libdir)

$(OBJDIR)/cfgsystem_build_config.o: cfgsystem_build_config/cfgsystem_build_config.c
	$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@ $(LIBS)

$(OBJDIR)/%.lo: %.c
	@[ -d $$(dirname $@) ] || \
		(mkdir -p $$(dirname $@))
	$(LIBTOOL) --mode=compile $(CC) $(CFLAGS) $(CPPFLAGS) -fPIC -shared -c $< -o $@

.PHONY: install
install: $(MODULE) $(TARGET)-init
	$(INSTALL) -m 775 -d $(DESTDIR)/$(libdir)
	$(LIBTOOL) --mode=install cp $(MODULE) $(DESTDIR)/$(libdir)/;
	$(INSTALL) -d $(NETOPEER_DIR)/ietf-system/
	@for i in $(MODEL); do \
		$(INSTALL_DATA) -m 600 $$i $(NETOPEER_DIR)/ietf-system/; \
	done
	@if test -n "$(NETOPEER_MANAGER)"; then \
		if test "`$(NETOPEER_MANAGER) list | grep ietf-system`" = "ietf-system"; then \
			$(NETOPEER_MANAGER) rm --name ietf-system; \
		fi; \
		$(NETOPEER_MANAGER) add --name ietf-system \
			--model $(NETOPEER_DIR)/ietf-system/ietf-system.yin \
			--transapi $(DESTDIR)/$(libdir)/cfgsystem.so \
			--features authentication local-users ntp timezone-name \
			--datastore $(NETOPEER_DIR)/ietf-system/datastore.xml; \
	fi
	./$(TARGET)-init $(NETOPEER_DIR)/ietf-system/datastore.xml

.PHONY: clean
clean:
	$(LIBTOOL) --mode clean rm -f $(LOBJS)
	$(LIBTOOL) --mode clean rm -f $(MODULE)
	rm -rf $(MODULE) $(TARGET)-init $(OBJDIR)
	rm -rf cfgsystem_build_config/cfgsystem_build_config

